<template>
  <div>
    <Bread></Bread>
    <el-card class="box-card">
      <el-button type="primary" @click="add">添加角色</el-button>
      <el-table :data="list" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <dl v-for="item in scope.row.children" :key="item.id">
              <dt>
                <el-tag closable @close="close(scope.row,item.id)">{{item.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </dt>
              <dd>
                <div class="count" v-for="ite in item.children" :key="ite.id">
                  <div class="s">
                    <el-tag
                      closable
                      type="success"
                      @close="close(scope.row,ite.id)"
                    >{{ite.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </div>
                  <div class="x">
                    <el-tag
                      closable
                      type="warning"
                      v-for="it in ite.children"
                      :key="it.id"
                      @close="close(scope.row,it.id)"
                    >{{it.authName}}</el-tag>
                  </div>
                </div>
              </dd>
            </dl>
          </template>
        </el-table-column>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName" width="200px"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"  width="200px"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="primary" icon="el-icon-edit" @click="edit(scope.row)">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" @click="del(scope.row.id)">删除</el-button>
            <el-button type="warning" icon="el-icon-s-tools" @click="pei(scope.row.id)">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加角色 -->
    <el-dialog title="添加角色" :visible.sync="flag">
      <el-form :model="obj" :rules="rules" ref="obj">
        <el-form-item prop="roleName">
          <span>角色名称</span>
          <el-input v-model="obj.roleName"></el-input>
        </el-form-item>
        <el-form-item prop="roleDesc">
          <span>角色描述</span>
          <el-input v-model="obj.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="flag = false">取 消</el-button>
        <el-button type="primary" @click="adds(obj)">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 编辑角色 -->
    <el-dialog title="编辑角色" :visible.sync="flags">
      <el-form :model="obj" :rules="rules" ref="obj">
        <el-form-item prop="roleName">
          <span>角色名称</span>
          <el-input v-model="obj.roleName"></el-input>
        </el-form-item>
        <el-form-item prop="roleDesc">
          <span>角色描述</span>
          <el-input v-model="obj.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="flags = false">取 消</el-button>
        <el-button type="primary" @click="edits(obj)">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 角色授权 -->
    <el-dialog title="角色授权" :visible.sync="fla">
      <el-tree
        :data="arr"
        show-checkbox
        node-key="id"
        ref="tree"
        :props="defaultProps"
        default-expand-all
        :default-checked-keys="ad"
      ></el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button @click="fla = false">取 消</el-button>
        <el-button type="primary" @click="peis">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Bread from "@/components/breadmenu";
export default {
  components: { Bread },
  data() {
    return {
      ad: [],
      list: [],
      obj: {
        roleName: "",
        roleDesc: ""
      },
      arr: [],
      flag: false,
      flags: false,
      fla: false,
      rules: {
        roleName: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ],
        roleDesc: [
          { required: true, message: "请输入角色描述", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ]
      },
      defaultProps: {
        children: "children",
        label: "authName"
      },
      id: 0
    };
  },
  methods: {
    close(val, val1) {
      console.log(val, val1);

      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$http.delete("roles/" + val.id + "/rights/" + val1).then(res => {
            console.log(res);
            if (res.data.meta.status == 200) {
              this.$message({
                type: "success",
                message: "删除权限成功!"
              });
              val.children = res.data.data;
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    add() {
      this.flag = true;
    },
    adds(obj) {
      this.$refs.obj.validate(val => {
        if (val) {
          this.$http.post("roles", obj).then(res => {
            console.log(res);
            if (res.data.meta.status == 201) {
              this.$message({
                message: "添加成功",
                type: "success"
              });
              this.flag = false;
              this.user();
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    edit(info) {
      this.flags = true;
      this.obj = info;
    },
    edits(arrs) {
      console.log(arrs);
      this.$http.put("roles/" + arrs.id, this.obj).then(res => {
        console.log(res);
        if (res.data.meta.status == 200) {
          this.$message({
            message: "更新成功",
            type: "success"
          });
          this.flags = false;
          this.user();
        }
      });
    },
    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$http.delete("roles/" + id).then(res => {
            console.log(res);
            if (res.data.meta.status == 200) {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.user();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async user() {
      var { data: res } = await this.$http.get("roles");
      console.log(res);
      this.list = res.data;
    },
    async tree() {
      var { data: res } = await this.$http.get("rights/tree");  
      console.log(res.data);
      this.arr = res.data;
    },
    pei(id) {
      this.fla = true;
      this.id = id;
    },
    peis() {
      var as = [
        ...this.$refs.tree.getHalfCheckedKeys(),
        ...this.$refs.tree.getCheckedKeys()
      ];

      this.ad = as.join(",");
      console.log(this.ad);
      this.$http
        .post("roles/" + this.id + "/rights", { rids: this.ad })
        .then(res => {
          console.log(res);
          if (res.data.meta.status == 200) {
            this.$message({
              message: "权限设置成功",
              type: "success"
            });
            this.fla = false;
            this.user();
          }
        });
    }
  },
  mounted() {
    this.user();
    this.tree();
  }
};
</script>
<style lang="scss">
.box-card {
  margin-top: 10px;
}
dl {
  display: flex;
  width: 100%;
  height: 100%;

  dt {
    width: 15%;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid rgb(243, 240, 240);
  }
  dd {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    // border: 1px solid rgb(207, 207, 207);
    .count {
      display: flex;
      border-top: 1px solid rgb(243, 240, 240);
      box-sizing: border-box;
      padding: 10px 20px;
      .s {
        width: 15%;
        display: flex;
        align-items: center;
      }
      .x {
        margin-left: 20px;
        span {
          margin-left: 6px;
          margin: 3px;
        }
      }
    }
    .count:last-of-type {
      border-bottom: 1px solid rgb(243, 240, 240);
    }
  }
}
.el-form-item__content {
  display: flex;
  align-items: center;
  span {
    display: block;
    width: 80px;
  }
}
</style>